﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Drawing;
using Abhiruchi_Phase1.BAL_DAL.ORG_DAL;

namespace Abhiruchi_Phase1
{
    /// <summary>
    /// Interaction logic for UC_ORG_TaskMapping.xaml
    /// </summary>
    public partial class UC_ORG_TaskMapping : UserControl
    {
        public static string Mode = "";
        public static string frselectionChngd = "start";

        public UC_ORG_TaskMapping()
        {          
            InitializeComponent();
            InitilizeFormData();
            AddBlankRows();
        }

        #region Add Blank rows to Gridview
        private void AddBlankRows()
        {
            grdDisp.ItemsSource = null;
            DataTable mydtMappingHistory = new DataTable();
            DataRow dtrow;
            for (int i = 0; i < 15; i++)
            {
                dtrow = mydtMappingHistory.NewRow();
                mydtMappingHistory.Rows.Add(dtrow);
            }
            grdDisp.ItemsSource = mydtMappingHistory;
        } 
        #endregion

        #region Default From Data
        private void InitilizeFormData()
        {
            lblMode.Content = "New";
            GetTaskMappingIdentity();
            DataTable mydtSec = ORGTaskMappingMasterDAL.GetDBDetails(6);

            if (mydtSec.Rows.Count > 0)
            {
                cmbMInputSec.DataContext = mydtSec.DefaultView;
                cmbMInputSec.DisplayMemberPath = mydtSec.Columns["SectionName"].ToString();
                cmbMInputSec.SelectedValuePath = mydtSec.Columns["SectionId"].ToString();
            }
            cmbMInputEmpType.Items.Add("Select");
            cmbMInputEmpType.Items.Add("Permanent");
            cmbMInputEmpType.Items.Add("Contract");
            cmbMInputEmpType.SelectedIndex = 0;

            dpInputMStartDate.SelectedDate = DateTime.Now;
            dpInputMEndDate.SelectedDate = DateTime.Now.AddMonths(1);

            chkInputAutoRenew.IsChecked = true;
            chkMIsActive.IsChecked = true;
        }

        public void GetTaskMappingIdentity()
        {
            DataTable mydtTskMapID = ORGTaskMappingMasterDAL.GetDBDetails(7);
            if (mydtTskMapID.Rows.Count > 0)
            {
                if (mydtTskMapID.Rows.Count.ToString() == "0")
                {                    
                    lblInputMid.Text ="1";
                }
                else
                {
                    lblInputMid.Text = (Int32.Parse(mydtTskMapID.Rows[0][0].ToString()) + 1).ToString();
                }
            }
        }
        
        #endregion

        #region Save Details
        private void btnMSave_Click(object sender, RoutedEventArgs e)
        {
            if (validationSucess())
            {

                bool checkEmp = true;
                MessageBoxResult result = MessageBoxResult.No;
                string strRenew = "";
                int renew;
                string strStatus = "";
                int status;
                if (chkInputAutoRenew.IsChecked == true)
                {
                    strRenew = "Yes";
                    renew = 1;
                }
                else
                {
                    strRenew = "No";
                    renew = 0;
                }
                if (chkMIsActive.IsChecked == true)
                {
                    strStatus = "Yes";
                    status = 1;
                }
                else
                {
                    strStatus = "No";
                    status = 0;
                }
                string strtDate = ((DateTime)dpInputMStartDate.SelectedDate).ToShortDateString();
                string endDate = ((DateTime)dpInputMEndDate.SelectedDate).ToShortDateString();
                DataTable mydtMapping = ORGTaskMappingMasterDAL.GetDetailsByNameFromMappingMaster(((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), 11);
                if (mydtMapping.Rows.Count > 0)
                {
                    if (cmbMInputSec.Text != mydtMapping.Rows[0][1].ToString() || cmbInputMtskName.Text != mydtMapping.Rows[0][2].ToString() || strtDate != (Convert.ToDateTime(mydtMapping.Rows[0][3].ToString())).ToShortDateString() || endDate != (Convert.ToDateTime(mydtMapping.Rows[0][4].ToString())).ToShortDateString())
                    {
                        result = (MessageBox.Show("Do you want to Assign a new Task for " + cmbInputMEmpName.Text, "Information", MessageBoxButton.YesNo, MessageBoxImage.Information));

                        if (result == MessageBoxResult.Yes)
                        {
                            DataTable mydtInsert = ORGTaskMappingMasterDAL.UpdateAndInsert(((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), 1);
                            DataTable mydtUpdate = ORGTaskMappingMasterDAL.UpdateAndInsert(Int32.Parse(((DataRowView)cmbMInputSec.Items[cmbMInputSec.SelectedIndex]).Row[0].ToString()), Int32.Parse(((DataRowView)cmbInputMtskName.Items[cmbInputMtskName.SelectedIndex]).Row[1].ToString()), ((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), Convert.ToDateTime(strtDate), Convert.ToDateTime(endDate), renew, txtInputMRemarks.Text, Convert.ToDateTime(System.DateTime.Now.ToShortDateString()), status, 3);                          
                        }
                    }
                    else
                    {
                        DataTable mydtInsert = ORGTaskMappingMasterDAL.UpdateDetails(Int32.Parse(((DataRowView)cmbMInputSec.Items[cmbMInputSec.SelectedIndex]).Row[0].ToString()), Int32.Parse(((DataRowView)cmbInputMtskName.Items[cmbInputMtskName.SelectedIndex]).Row[1].ToString()), cmbMInputEmpType.Text, ((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), Convert.ToDateTime(strtDate), Convert.ToDateTime(endDate), renew, txtInputMRemarks.Text, status, 12);                       
                    }

                }
                else
                {
                    DataTable mydtSave = ORGTaskMappingMasterDAL.InsertDetails(Int32.Parse(((DataRowView)cmbMInputSec.Items[cmbMInputSec.SelectedIndex]).Row[0].ToString()), Int32.Parse(((DataRowView)cmbInputMtskName.Items[cmbInputMtskName.SelectedIndex]).Row[1].ToString()), cmbMInputEmpType.Text, ((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), Convert.ToDateTime(strtDate), Convert.ToDateTime(endDate), renew, txtInputMRemarks.Text, status, 10);                   
                }
                MessageBox.Show(cmbInputMEmpName.Text + " Details Saved Sucessfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                btnMClear_Click(this, e);
            }

        } 
        #endregion

        #region Validation
        private bool validationSucess()
        {
            lblMEmpType.Foreground = System.Windows.Media.Brushes.Black;
            lblMEmpName.Foreground = System.Windows.Media.Brushes.Black;
            lblMSecName.Foreground = System.Windows.Media.Brushes.Black;
            lblMtskName.Foreground = System.Windows.Media.Brushes.Black;
            lblMStrtDate.Foreground = System.Windows.Media.Brushes.Black;
            lblMEndDate.Foreground = System.Windows.Media.Brushes.Black;
            if (cmbMInputEmpType.SelectedIndex == 0)
            {
                MessageBox.Show("Please Select Employee Type", "Employee Type", MessageBoxButton.OK, MessageBoxImage.Warning);
                cmbMInputEmpType.Focus();
                lblMEmpType.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblMEmpType.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (cmbInputMEmpName.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Employee Name", "Employee Name", MessageBoxButton.OK, MessageBoxImage.Warning);
                cmbInputMEmpName.Focus();
                lblMEmpName.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblMEmpName.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (cmbMInputSec.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Section", "Section", MessageBoxButton.OK, MessageBoxImage.Warning);
                cmbMInputSec.Focus();
                lblMSecName.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblMSecName.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (cmbInputMtskName.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Task Name", "Task Name", MessageBoxButton.OK, MessageBoxImage.Warning);
                cmbInputMtskName.Focus();
                lblMtskName.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblMtskName.Foreground = System.Windows.Media.Brushes.Black;
            }           
            if (dpInputMEndDate.SelectedDate <= dpInputMStartDate.SelectedDate)
            {
                MessageBox.Show("Please Select the End date Greater than Start Date", "End date", MessageBoxButton.OK, MessageBoxImage.Warning);
                dpInputMEndDate.Focus();
                lblMEndDate.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblMEndDate.Foreground = System.Windows.Media.Brushes.Black;
            }
            return true;
        }
        #endregion

        #region Clear Form Controls
        private void btnMClear_Click(object sender, RoutedEventArgs e)
        {

            lblMode.Content = "New";
            GetTaskMappingIdentity();
            cmbInputMEmpName.SelectedIndex = -1;
            cmbMInputEmpType.SelectedIndex = 0;
            cmbInputMtskName.SelectedIndex = -1;
            cmbMInputSec.SelectedIndex = -1;
            cmbInputMtskName.DataContext = null;
            cmbInputMtskName.SelectedIndex = -1;
            dpInputMStartDate.SelectedDate = DateTime.Now;
            dpInputMEndDate.SelectedDate = DateTime.Now.AddMonths(1);
            chkInputAutoRenew.IsChecked = true;
            txtInputMRemarks.Text = "";
            chkMIsActive.IsChecked = true;
            AddBlankRows();
        } 
        #endregion

        #region EmployeeType Selection Change
        private void cmbMInputEmpType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataTable mydtEmpType = ORGTaskMappingMasterDAL.GetDBDetails(cmbMInputEmpType.SelectedValue.ToString(), 9);
            cmbInputMEmpName.DataContext = mydtEmpType.DefaultView;
            cmbInputMEmpName.DisplayMemberPath = mydtEmpType.Columns["EName"].ToString();
            cmbInputMEmpName.SelectedValuePath = mydtEmpType.Columns["EId"].ToString();
        } 
        #endregion

        #region EmployeeName Selection Changed
        private void cmbInputMEmpName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbInputMEmpName.SelectedIndex == -1)
            {
                return;
            }

            DataTable mydtMapping = ORGTaskMappingMasterDAL.GetDetailsByNameFromMappingMaster(((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), 11);
            lblMode.Content = "Edit";
            grdDisp.ItemsSource = null;
            DataTable mydtMappingHistory = ORGTaskMappingMasterDAL.GetDetailsByNameFromMappingHistoryMaster(((DataRowView)cmbInputMEmpName.Items[cmbInputMEmpName.SelectedIndex]).Row[1].ToString(), 2);
            DataRow dtrow;
            for (int i = 0; i < 15; i++)
            {
                dtrow = mydtMappingHistory.NewRow();
                dtrow["HistoryID"] = DBNull.Value;
                dtrow["EName"] = "";
                dtrow["SectionName"] = "";
                dtrow["TaskName"] = "";
                dtrow["StartDate"] = DBNull.Value;
                dtrow["EndDate"] = DBNull.Value;
                dtrow["Auto Renew"] = "";
                dtrow["Remarks"] = "";
                mydtMappingHistory.Rows.Add(dtrow);
            }

            if (mydtMappingHistory.Rows.Count > 0)
            {
                grdDisp.ItemsSource = mydtMappingHistory;
            }
            if (mydtMapping.Rows.Count > 0)
            {
                cmbMInputSec.Text = mydtMapping.Rows[0][1].ToString();                
                dpInputMStartDate.SelectedDate = Convert.ToDateTime(mydtMapping.Rows[0][3].ToString());
                dpInputMEndDate.SelectedDate = Convert.ToDateTime(mydtMapping.Rows[0][4].ToString());
                if (mydtMapping.Rows[0][5].ToString() == "Yes")
                {
                    chkInputAutoRenew.IsChecked = true;
                }
                else
                {
                    chkInputAutoRenew.IsChecked = false;
                }
                txtInputMRemarks.Text = mydtMapping.Rows[0][6].ToString();

                if (mydtMapping.Rows[0][7].ToString() == "Yes")
                {
                    chkMIsActive.IsChecked = true;
                }
                else
                {
                    chkMIsActive.IsChecked = false;
                }
            }
            else
            {
                cmbInputMtskName.SelectedIndex = -1;
                cmbMInputSec.SelectedIndex = -1;
                cmbInputMtskName.SelectedIndex = -1;
                dpInputMStartDate.SelectedDate = DateTime.Now;
                dpInputMEndDate.SelectedDate = DateTime.Now.AddMonths(1);
                chkInputAutoRenew.IsChecked = true;
                txtInputMRemarks.Text = "";
                chkMIsActive.IsChecked = true;
            }
        } 
        #endregion

        #region Start Date Changed
        private void dpInputMStartDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            dpInputMEndDate.SelectedDate = ((DateTime)dpInputMStartDate.SelectedDate).AddMonths(1);
        } 
        #endregion

        #region Section Changed
        private void cmbMInputSec_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           
            if (cmbMInputSec.SelectedIndex != -1)
            {
                DataTable mydtTskNm = ORGTaskMappingMasterDAL.GetDBDetailsOfTaskName(13,cmbMInputSec.SelectedValue.ToString());                
                cmbInputMtskName.DataContext = mydtTskNm;                
                cmbInputMtskName.DisplayMemberPath = mydtTskNm.Columns["TaskName"].ToString();
                cmbInputMtskName.SelectedValuePath = mydtTskNm.Columns["TaskID"].ToString();
            }
        } 
        #endregion
    
    }
}
